package com.furnitur.modules.admin.service.order.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.furnitur.common.exception.BusinessException;
import com.furnitur.core.entity.order.Order;
import com.furnitur.core.entity.order.OrderStatus;
import com.furnitur.core.mapper.order.OrderMapper;
import com.furnitur.modules.admin.service.order.OrderService;
import com.furnitur.modules.admin.vo.order.OrderStatisticsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    
    @Override
    public Page<Order> getOrderPage(Integer pageNum, Integer pageSize, String orderNo, Integer status) {
        return lambdaQuery()
            .eq(StringUtils.hasText(orderNo), Order::getOrderNo, orderNo)
            .eq(status != null, Order::getStatus, status)
            .orderByDesc(Order::getCreatedAt)
            .page(new Page<>(pageNum, pageSize));
    }

    @Override
    public Order getByOrderNo(String orderNo) {
        Order order = lambdaQuery()
            .eq(Order::getOrderNo, orderNo)
            .one();
            
        if (order == null) {
            throw new BusinessException("订单不存在");
        }
        return order;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ship(String orderNo, String trackingNo) {
        Order order = getByOrderNo(orderNo);
        
        if (order.getStatus() != OrderStatus.PENDING_SHIPMENT.getValue()) {
            throw new BusinessException("订单状态不正确");
        }
        
        order.setStatus(OrderStatus.SHIPPED.getValue());
        order.setTrackingNo(trackingNo);
        order.setShippingTime(new Date());
        updateById(order);
    }

    @Override
    public OrderStatisticsVO getStatistics() {
        OrderStatisticsVO vo = new OrderStatisticsVO();
        
        // 获取订单总数
        long totalOrders = count();
        vo.setTotalOrders((int) totalOrders);
        
        // 获取待发货订单数
        long pendingShipOrders = lambdaQuery()
            .eq(Order::getStatus, OrderStatus.PENDING_SHIPMENT.getValue())
            .count();
        vo.setPendingShipOrders((int) pendingShipOrders);
        
        // 获取已发货订单数
        long shippedOrders = lambdaQuery()
            .eq(Order::getStatus, OrderStatus.SHIPPED.getValue())
            .count();
        vo.setShippedOrders((int) shippedOrders);
        
        // 获取订单总金额
        vo.setTotalAmount(baseMapper.sumOrderAmount(null));
        
        // 获取每日订单统计
        vo.setDailyStats(baseMapper.getDailyOrderStats(null));
        
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmReceipt(String orderNo) {
        Order order = getByOrderNo(orderNo);
        
        if (order.getStatus() != OrderStatus.SHIPPED.getValue()) {
            throw new BusinessException("订单状态不正确");
        }
        
        order.setStatus(OrderStatus.COMPLETED.getValue());
        order.setCompletionTime(new Date());
        updateById(order);
    }
} 